<script lang="ts" setup>
import { addRoom, getRoomType } from '@/api/room';
import type { roomType, roomTypes } from '@/props/room';
import { useRoomStore } from '@/stores/room';
import { ElMessage } from 'element-plus';
import { onMounted, reactive, } from 'vue';

const roomStore = useRoomStore()
const roomForm = reactive<roomType>({
  roomTypeId:null,
  roomName: ''
})

//获取房间类型
const roomTypeList = reactive<roomTypes>([])
const getRoomTypeData = async() => {
  const {data} = await getRoomType()
  roomTypeList.push(...data.data)
}
onMounted(() => {
  getRoomTypeData()
})

const isroomFormEmpty = (roomForm:roomType) => {
  if(roomForm.roomTypeId === null || roomForm.roomName ==='') return true
  return false
}

//
const addRoomData = async() => {
  if(isroomFormEmpty(roomForm)) return ElMessage.error("房间名|房间类型不能为空")
  const {data} = await addRoom(roomForm)
  if(data.code===1) {
    roomStore.dataUserRoom()
    return ElMessage.success("房间添加成功")
  }
  return ElMessage.error("房间添加失败")
}

const formLabelWidth = '150px'
</script>

<template>
  <div>
    <el-dialog v-model="roomStore.showOnPage" title="添加房间" width="500">
      <el-form :model="roomForm">
        <el-form-item label="房间名" :label-width="formLabelWidth">
          <el-input v-model="roomForm.roomName" autocomplete="off" />
        </el-form-item>
        <el-form-item label="房间类型" :label-width="formLabelWidth">
          <el-select v-model="roomForm.roomTypeId" placeholder="请选择房间类型">
              <el-option v-for="roomType in roomTypeList" :key="roomType.roomTypeId" :label="roomType.roomName" :value="roomType.roomTypeId"/>
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="roomStore.showOnPage=false">取消</el-button>
          <el-button type="primary" @click="addRoomData">
            提交
          </el-button>
        </div>
      </template>
  </el-dialog>
</div>
</template>

<style lang="scss" scoped>
:deep(.el-dialog) {
  background-color: rgb(47, 59, 82);
}

:deep(.el-dialog__title){
    color:white;
    margin-left: 50px;
  }
</style>
